package org.xiepengfei.qimowang.commons;

import java.sql.ResultSet;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;




public class FileDao {
	private DB mydb=null;
	public FileDao(){
		mydb=new DB();
	}
	public int addFileInfo(FileBean file){
		int i=-1;		
		String sql="insert into tb_file (file_savename,file_filename,file_filetype,file_filesize,file_fileinfo,file_uptime,file_type,file_belong) values (?,?,?,?,?,?,?,?)";
		Object[] params={file.getSaveName(),file.getFileName(),file.getFileType(),file.getFileSize(),file.getFileInfo(),file.getUptime(),file.getType(),file.getBelong()};
		
		
		mydb.doPstm(sql, params);
		
		try {
			i=mydb.getCount();
		} catch (SQLException e) {
			i=-1;
			e.printStackTrace();
		}
		return i;		
	}
	public FileBean getFileSingle(String savename) throws SQLException{
		FileBean single=null;
		String sql="select * from tb_file where file_savename=?";
		Object[] params={savename};
		List list=getList(sql,params);
		if(list!=null&&list.size()!=0)
			single=(FileBean)list.get(0);
		return single;
	}
	public List getFileList() throws SQLException{
		String sql="select * from tb_file order by file_savename desc";
		List list=getList(sql,null);
		return list;
	}
	public List getjianliListBybelong(String belong) throws SQLException{
		String sql="select * from tb_file where file_type='jianli' and file_belong='"+belong+"' order by file_savename desc";
		List list=getList(sql,null);
		return list;
	}
	public List getbaogaoListBybelong(String belong) throws SQLException{
		String sql="select * from tb_file where file_type='baogao' and file_belong='"+belong+"' order by file_savename desc";
		List list=getList(sql,null);
		return list;
	}
	public List getzhizhaoListBybelong(String belong) throws SQLException {
		String sql="select * from tb_file where file_type='zhizhao' and file_belong='"+belong+"' order by file_savename desc";
		List list=getList(sql,null);
		return list;
	}
	
	private List getList(String sql,Object[] params) throws SQLException{
		List list=null;
		mydb.doPstm(sql, params);
		ResultSet rs=mydb.getRs();
		if(rs!=null){
			list=new ArrayList();
			while(rs.next()){
				FileBean single=new FileBean();
				single.setId(rs.getInt(1));
				single.setSaveName(rs.getString(2));
				single.setFileName(rs.getString(3));
				single.setFileType(rs.getString(4));
				single.setFileSize(rs.getInt(5));
				single.setFileInfo(rs.getString(6));
				single.setUptime(StringHandler.timeTostr(rs.getTimestamp(7)));
				single.setType(rs.getString(8));
				single.setBelong(rs.getString(9));
				list.add(single);
			}
			rs.close();
		}
		return list;
	}
	
	public void closed(){
		mydb.closed();
	}

}
